﻿using Humanizer;
using System.Collections.Generic;
using System.Linq;

namespace  Msp.Scaffolding.Entities
{
    public class EntityInfo
    {
        public EntityInfo(string @namespace, string name, string baseType, string primaryKey, string relativeDirectory)
        {
            Namespace = @namespace;
            Name = name;
            BaseType = baseType;
            PrimaryKey = primaryKey;
            RelativeDirectory = relativeDirectory.Replace('\\', '/');
        }

        /// <summary>
        /// 模块名
        /// </summary>
        public string ModuleName { get; set; }


        /// <summary>
        /// 实体类应用服务等的命名空间
        /// </summary>
        public string Namespace { get; }
        public string RelativeNamespace => RelativeDirectory.Replace('/', '.');
        public string RelativeDirectory { get; }
        public string NamespaceLastPart => Namespace.Split('.').Last();
        public string Name { get; }


        public string TName
        {
            get
            {
                return Name.Replace("Entity", "");
            }
        }
        /// <summary>
        /// 小写驼峰
        /// </summary>
        public string TNameCamelize => TName.Camelize();

        /// <summary>
        /// 复数
        /// </summary>
        public string NamePluralized => Name.Pluralize();
        /// <summary>
        /// 首字母小写
        /// </summary>
        public string NameCamelize => Name.Camelize();

        /// <summary>
        /// 小写+复数
        /// </summary>
        public string NameCamelizePluralized => Name.Camelize().Pluralize();

        /// <summary>
        /// 继承的接口，可以多个接口
        /// </summary>
        public string[] Interfaces { get; set; }

        /// <summary>
        /// 继承的类
        /// </summary>
        public string BaseType { get; }


        /// <summary>
        /// 主键类型
        /// </summary>
        public string PrimaryKey { get; }

        public string PrimaryKeyTsType
        {
            get
            {
                if (PrimaryKey == "long" || PrimaryKey == "int" || PrimaryKey == "double" || PrimaryKey == "float")
                    return "number";
                else if (PrimaryKey == "string" || PrimaryKey == "String") return "string";
                return "any";
            }
        }

        /// <summary>
        /// 主键名称
        /// </summary>
        public string PrimaryKeyName { get; set; }

        /// <summary>
        /// 主键小写
        /// </summary>
        public string PrimaryKeyNameCame => PrimaryKeyName.Camelize();

        /// <summary>
        /// 类的属性键值对。
        /// </summary>
        public List<PropertyInfo> Properties { get; } = new List<PropertyInfo>();

        /// <summary>
        /// 是否是树实体
        /// </summary>
        public bool HasTree
        {
            get
            {
                return this.Properties.Any(x => x.Name.ToLower() == "ParentId".ToLower());
            }
        }

        /// <summary>
        /// 是否有启停用
        /// </summary>
        public bool HasDisabled
        {
            get
            {
                if (this.BaseType?.Contains("Disable")==true)
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// 是否有排序字段
        /// </summary>
        public bool HasOrderIndex
        {
            get
            {
                if (this.Properties.Any(x => x.Name.Contains("OrderIndex")))
                {
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// 是否有软删除
        /// </summary>
        public bool HasSoftDelete
        {
            get
            {
                if (this.BaseType?.Contains("BaseIdEntity")==true)
                {
                    return false;
                }
                return true;
            }
        }
        /// <summary>
        /// 类的备注
        /// </summary>
        public string EntityRemark { get; set; } = "";
    }
}
